package com.happy.study.servier.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.study.dao.UserMapper;
import com.happy.study.entity.UserEntity;
import com.happy.study.servier.IUserService;
import com.happy.study.util.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private MailProperties mailProperties;
    @Resource
    private JavaMailSender javaMailSender;

    /**
     * 根据用户id获取用户对象，获取当前登录用户对象
     *
     * @param userEntity
     * @return
     */
    @Override
    public UserEntity queryById ( UserEntity userEntity ) {
        if ( userEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( userEntity.getId () == null ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        userEntity = userMapper.queryById ( userEntity.getId () );
        return userEntity;
    }

    @Override
    public UserEntity querySingle ( UserEntity userEntity ) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<> ();
        if ( userEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( userEntity.getId () != null ) {
            queryWrapper.eq ( "id", userEntity.getId () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getUserName () ) ) {
            queryWrapper.eq ( "userName", userEntity.getUserName () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getPassword () ) ) {
            queryWrapper.eq ( "password", userEntity.getPassword () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getNickName () ) ) {
            queryWrapper.like ( "nickName", userEntity.getNickName () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getPhone () ) ) {
            queryWrapper.eq ( "phone", userEntity.getPhone () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getEmail () ) ) {
            queryWrapper.eq ( "email", userEntity.getEmail () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getCompany () ) ) {
            queryWrapper.like ( "company", userEntity.getCompany () );
        }
        if ( userEntity.getSex () != null ) {
            queryWrapper.eq ( "sex", userEntity.getSex () );
        }
        userEntity = baseMapper.selectOne ( queryWrapper );
        return userEntity;
    }

    @Override
    public UserEntity updateSingle ( UserEntity userEntity ) {
        UpdateWrapper<UserEntity> updateWrapper = new UpdateWrapper<> ();
        if ( userEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( userEntity.getId () == null ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        if ( StringUtils.isNotBlank ( userEntity.getUserName () ) ) {
            updateWrapper.eq ( "userName", userEntity.getUserName () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getNickName () ) ) {
            updateWrapper.eq ( "nickName", userEntity.getNickName () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getPhone () ) ) {
            updateWrapper.eq ( "phone", userEntity.getPhone () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getEmail () ) ) {
            updateWrapper.eq ( "email", userEntity.getEmail () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getCompany () ) ) {
            updateWrapper.eq ( "company", userEntity.getCompany () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getAddress () ) ) {
            updateWrapper.eq ( "address", userEntity.getAddress () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getSign () ) ) {
            updateWrapper.eq ( "sign", userEntity.getSign () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getHeadImgUrl () ) ) {
            updateWrapper.eq ( "headimgurl", userEntity.getHeadImgUrl () );
        }
        if ( userEntity.getSex () != null ) {
            updateWrapper.eq ( "sex", userEntity.getSex () );
        }
        if ( userEntity.getAddress () != null ) {
            updateWrapper.eq ( "address", userEntity.getAddress () );
        }
        userEntity.setChangeTime ( new Date () );
        int i = baseMapper.updateById ( userEntity );
        if ( i == 0 ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        return userEntity;
    }

    /**
     * 发送邮件
     * 如果邮箱存在则直接登录，如果不存在注册后登录，下次直接登录保留信息
     * @param userEntity
     * @return
     */
    @Override
    public String sendVerification ( UserEntity userEntity ) {
        UserEntity user = baseMapper.selectOne ( new QueryWrapper<UserEntity> ().eq ( "email", userEntity.getEmail () ) );
        if ( user == null ) {
            userEntity.setCreateTime ( new Date () );
            int insert = baseMapper.insert ( userEntity );
            if ( insert == 0 ) {
                return null;
            }
        }
        String inviteCode = CommonUtil.getInviteCode ( 6 );
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage ();
        simpleMailMessage.setFrom ( mailProperties.getUsername () );
        simpleMailMessage.setTo ( userEntity.getEmail () );
        simpleMailMessage.setText ( "验证码：" + inviteCode );
        simpleMailMessage.setSubject ( "Happy学习平台 - 登录验证码" );
        javaMailSender.send ( simpleMailMessage );
        return inviteCode;
    }
}
